using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Media;
using Common;
using Common.Enum;
using SProject;

namespace ProjectView.Windows
{
	public partial class ProjectSampleTypeEditWindow : Window, IComponentConnector
	{
		public ObservableCollection<CustomSampleType> CustomSampleTypeList = new ObservableCollection<CustomSampleType>();

		private Project _project;

		public ProjectSampleTypeEditWindow(Project project)
		{
			_project = project;
			InitializeComponent();
			dataGrid.ItemsSource = CustomSampleTypeList;
			Restore();
			colSampleType.CellEditingTemplateSelector = new DataGridCellEditSelector
			{
				Template = colSampleType.CellTemplate
			};
		}

		private void Restore()
		{
			CustomSampleTypeList.Clear();
			foreach (CustomSampleType customSampleType in _project.CustomSampleTypeList)
			{
				CustomSampleTypeList.Add(new CustomSampleType
				{
					Visible = customSampleType.Visible,
					SampleType = customSampleType.SampleType,
					SampleTypeName = customSampleType.SampleTypeName,
					Color = customSampleType.Color,
					CanEditor = (customSampleType.SampleType == SampleType.Custom)
				});
			}
			dataGrid.SelectedIndex = 0;
		}

		private void RefreshButtonStatus()
		{
			btnDelete.IsEnabled = false;
			btnUp.IsEnabled = false;
			btnDown.IsEnabled = false;
			int selectedIndex = dataGrid.SelectedIndex;
			if (selectedIndex >= 0 && selectedIndex < CustomSampleTypeList.Count)
			{
				if (CustomSampleTypeList[selectedIndex].SampleType == SampleType.Custom)
				{
					btnDelete.IsEnabled = true;
				}
				if (selectedIndex > 0)
				{
					btnUp.IsEnabled = true;
				}
				if (selectedIndex < CustomSampleTypeList.Count - 1)
				{
					btnDown.IsEnabled = true;
				}
			}
		}

		private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			RefreshButtonStatus();
		}

		private void btnDelete_Click(object sender, RoutedEventArgs e)
		{
			dataGrid.EndEdit();
			int num = dataGrid.SelectedIndex;
			if (num >= 0 && num < CustomSampleTypeList.Count)
			{
				CustomSampleTypeList.RemoveAt(num);
				if (num >= CustomSampleTypeList.Count)
				{
					num = CustomSampleTypeList.Count - 1;
				}
				dataGrid.SelectedIndex = num;
			}
		}

		private void btnAdd_Click(object sender, RoutedEventArgs e)
		{
			dataGrid.EndEdit();
			CustomSampleTypeList.Add(new CustomSampleType
			{
				Visible = true,
				SampleType = SampleType.Custom,
				SampleTypeName = string.Empty,
				Color = Colors.Black,
				CanEditor = true
			});
			dataGrid.SelectedIndex = dataGrid.Items.Count - 1;
		}

		private void btnUp_Click(object sender, RoutedEventArgs e)
		{
			dataGrid.EndEdit();
			int selectedIndex = dataGrid.SelectedIndex;
			if (selectedIndex >= 0 && selectedIndex < CustomSampleTypeList.Count)
			{
				CustomSampleTypeList.Move(selectedIndex, selectedIndex - 1);
				RefreshButtonStatus();
			}
		}

		private void btnDown_Click(object sender, RoutedEventArgs e)
		{
			dataGrid.EndEdit();
			int selectedIndex = dataGrid.SelectedIndex;
			if (selectedIndex >= 0 && selectedIndex < CustomSampleTypeList.Count)
			{
				CustomSampleTypeList.Move(selectedIndex, selectedIndex + 1);
				RefreshButtonStatus();
			}
		}

		private void btnRestore_Click(object sender, RoutedEventArgs e)
		{
			dataGrid.EndEdit();
			_project.RestoreCustomSampleType();
			Restore();
		}

		private void btnOK_Click(object sender, RoutedEventArgs e)
		{
			dataGrid.EndEdit();
			int num = 0;
			int num2 = 0;
			foreach (CustomSampleType customSampleType in CustomSampleTypeList)
			{
				string sampleTypeName = customSampleType.SampleTypeName;
				if (string.IsNullOrEmpty(sampleTypeName))
				{
					num2++;
					break;
				}
				foreach (CustomSampleType customSampleType2 in CustomSampleTypeList)
				{
					if (customSampleType2.SampleTypeName.Equals(sampleTypeName))
					{
						num++;
					}
				}
			}
			if (num2 > 0)
			{
				MessageBox.Show(Utility.GetMessage("msg_genoname_is_required"));
				return;
			}
			if (num > CustomSampleTypeList.Count)
			{
				MessageBox.Show(Utility.GetMessage("msg_genoname_is_repeat"));
				return;
			}
			base.DialogResult = true;
			Close();
		}
	}
}
